# -*- conding:UTF-8 -*-

'''
@ Project: p6_0518
@ File: serializer.py
@ Author: 韩成瑾
@ Date：2022/5/19 9:02

'''

from rest_framework import serializers
from users.models import *

def yourfunc(name):
    ...
    return name
    # raise ValidationError('')
# 部门
class DepartmentSer(serializers.Serializer):
    id = serializers.IntegerField(label='部门',read_only=True)
    name = serializers.CharField(label='部门名称',max_length=30,min_length=2,default='AI',allow_null=True,
                                 trim_whitespace=True,validators=[yourfunc,])
    def validate(self, attrs):
        return attrs
    def create(self, validated_data):
        print('这是我自己定义的create方法')
        dep = Department.objects.create(**validated_data)
        return dep
    def updte(self,instance,validated_data):
        print('这是我写的update方法')
        instance.name = validated_data.get('name')
        instance.save()
        return instance


class DepartmentModelSer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = '__all__'


class StaffSer(serializers.Serializer):
    department = serializers.PrimaryKeyRelatedField(label='部门',queryset=Department.objects.all())
    department_name = serializers.SerializerMethodField(read_only=True)
    duty = serializers.PrimaryKeyRelatedField(label='职责',queryset=Duty.objects.all())
    duty_name = serializers.SerializerMethodField(read_only=True)
    def get_duty_name(self,obj):
        return obj.duty.name
    class Meta:
        model = Staff
        fields = ['id','name','phone','department','duty','department_name']
        read_only_fields = ['id']



# 职责
class DutySer(serializers.ModelSerializer):
    class Meta:
        model = Duty
        fields = "__all__"

from datetime import datetime
# 用户信息
class UserSer(serializers.ModelSerializer):
    # 只用于反序列化输入
    password = serializers.CharField(write_only=True,trim_whitespace=True,allow_null=True)
    last_login = serializers.DateTimeField(default=datetime.now())

    def validate(self, attrs):
        #数据校验
        return attrs


    class Meta:
        model = User
        # 指明需要序列化操做的字段
        fields = ['id','username','password','mobile','email','date_joined','last_login']
        read_only_fields = ['id',]

# 头像图片
class HeadSer(serializers.ModelSerializer):
    head_name = serializers.SerializerMethodField(read_only=True)
    def get_head_name(self,obj):
        return obj.head.name
    class Meta:
        model = Head
        fields = '__all__'
        read_only_fields = ['id',]

# 权限
class PermissionSer(serializers.ModelSerializer):
    typename = serializers.SerializerMethodField(read_only=True)
    def get_typename(self,obj):
        return obj.type_name.name
    class Meta:
        model = Permission
        fields = '__all__'
        read_only_fields = ['id',]

# 角色
class PermissionTypeSer(serializers.ModelSerializer):
    class Meta:
        model = PermissionType
        fields = '__all__'
        read_only_fields = ['id',]











